package com.linkage.order.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.book.pojo.Book;
import com.linkage.book.service.BookService;
import com.linkage.order.pojo.Order;
import com.linkage.order.pojo.OrderLine;
import com.linkage.order.service.OrderService;
import com.linkage.user.interceptor.NeedLogin;
import com.linkage.user.pojo.User;

/**
 * @author XiaMingYu
 */
@Controller
@RequestMapping("/orderController")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private BookService bookService;

	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	@NeedLogin
	public String orderList(Model model) {
		List<Order> orders = this.orderService.queryAll();
		model.addAttribute("orders", orders);
		return "/order/list";
	}

	@RequestMapping(value = "/neworder/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String neworder(Model model, @PathVariable("orderId") String orderId) {
		Order order = this.orderService.findOrderById(orderId);
		model.addAttribute("order", order);
		return "/order/neworder";
	}

	@RequestMapping(value = "/orderline/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String orderLine(Model model, @PathVariable("orderId") String orderId) {
		OrderLine orderLine = this.orderService.loadOneOrderLine(orderId);
		model.addAttribute("orderLine", orderLine);
		return "/order/orderitem";
	}

	@RequestMapping(value = "/generateOrder/{bookId}", method = RequestMethod.GET)
	public String generateOrder(HttpServletRequest request,
			@PathVariable("bookId") int bookId) {
		User user = (User) request.getSession().getAttribute(
				"LOGIN_USER_KEY_IN_SESSION");
		if (user == null) {
			return "login/firstlogin";
		} else {
			Book book = this.bookService.loadOne(bookId);
			OrderLine orderLine = this.orderService.generateOrderLine(request,
					book);
			this.orderService.saveOrderLine(orderLine);
			return "redirect:/orderController/neworder/"
					+ orderLine.getOrderId();
		}
	}

	@RequestMapping(value = "/deleteOrder/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String delete(Order order, @PathVariable("orderId") String orderId) {
		this.orderService.deleteOrder(orderId);
		return "redirect:/orderController/orderlist";
	}

/*	@RequestMapping(value = "/generateOrder/{book}", method = RequestMethod.GET)
	@NeedLogin
	public String generateOrder(HttpServletRequest request, Book book) {
		this.orderService.saveOrderLine(this.orderService.generateOrderLine(
				request, book));
		return "redirect:/orderController/orderlist";
	}*/

	@RequestMapping(value = "/payOrder/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String pay(@PathVariable("orderId") String orderId) {
		this.orderService.payOrder(orderId);
		return "redirect:/orderController/neworder/{orderId}";
	}

	@RequestMapping(value = "/confirmView/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String confirmView(Model model,
			@PathVariable("orderId") String orderId) {
		Order order = this.orderService.loadOneOrder(orderId);
		model.addAttribute("order", order);
		return "/order/confirmView";
	}

	@RequestMapping(value = "/payView/{orderId}", method = RequestMethod.GET)
	@NeedLogin
	public String payView(Model model, @PathVariable("orderId") String orderId) {
		Order order = this.orderService.loadOneOrder(orderId);
		model.addAttribute("order", order);
		return "/order/pay";
	}

	@RequestMapping(value = "/payOrder/{orderId}", method = RequestMethod.POST)
	@NeedLogin
	public String pay(HttpServletRequest request,
			@PathVariable("orderId") String orderId) {
		this.orderService.payOrder(orderId);
		return "redirect:/orderController/orderlist";
	}

	@RequestMapping(value = "/confirm/{orderId}", method = RequestMethod.POST)
	@NeedLogin
	public String confirm(Order order) {
		this.orderService.confirm(order);
		return "redirect:/orderController/orderlist";
	}
}
